// ActionScript file
import Modules.Login.ChangePasswordPopUp;
import Modules.Login.LoginInfo;

import com.desidea.common.Base64;
import com.desidea.components.spinner.BusySpinner;
import com.desidea.webservice.PartnerService;
import com.desidea.webservice.ProcessServiceRequestResultEvent;

import mx.containers.TitleWindow;
import mx.controls.Alert;
import mx.core.Application;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

private var _links:XML;

[Event(name=LOGON_SUCCESSFUL_EVENT, type="mx.rpc.events.ResultEvent")]

private function initLogin():void{
//	this.txtFooter.text = ApplicationInfo.appVersion;
}

private function btnLogin_Click():void{
	
	BusySpinner.getInstance().play(Application.application.ModuleCvs);

 	this.lblError.text = "";
	var param:String = 	"OPERATION=AUTHENTICATE_USER" +
						"&LOGINID=" + escape(txtEmailId.text) +
						"&PASSWORD=" + escape( Base64.encode( this.txtPassword.text ));
	
	var loginWS:PartnerService = new PartnerService();
	loginWS.addprocessServiceRequestEventListener(LoginWSResultHandler);
	loginWS.addPartnerServiceFaultEventListener( LoginWSFaultHandler);
	loginWS.processServiceRequest(param);
		
}

private function LoginWSResultHandler(event:ProcessServiceRequestResultEvent):void{
	ProcessLoginRespose( XML(event.result));
}

private function ProcessLoginRespose(response:XML):void{

	BusySpinner.getInstance().stop();
	Alert.show(""+response.toString());
	if(response.RESPONSE.RESULT== "E"){
		lblError.text = response.RESPONSE.MESSAGE;
		this.txtPassword.text = "";
		this.txtPassword.setFocus();
	}else if(response.RESPONSE.RESULT == "S"){
		var re:ResultEvent = new ResultEvent("LogonSuccessful",false,true, new LoginInfo(response) );
		this.dispatchEvent( re );
		this.txtEmailId.text = "";
		this.txtPassword.text = "";
		this.lblError.text = "";
	}else{
		lblError.text = "Unknown Error : Response Result not handled";
	}
}

private function LoginWSFaultHandler(event:FaultEvent):void{
	BusySpinner.getInstance().stop();
	Alert.show(event.fault.message);
}

private function OnClickAction(actionType:String):void{
	if(actionType == "changepassword"){
		var cpPopUp:TitleWindow = new ChangePasswordPopUp();
		PopUpManager.addPopUp(cpPopUp,this,true);
	}else{
		Alert.show("No Action Type Defined");	
	}
}
